package com.dsit.sq;

import android.app.Application;
import android.util.Log;

import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;

import protocol.ProtoFilter;
import tcp.TcpClientSocketThread;

/**
 * Created by Administrator on 2016/11/30.
 */

public class App extends Application {
    public static App instance;
    public boolean deviceConnected;
    int receiveCount = 0;

    TcpClientSocketThread tcpClientSocketThread;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        final ProtoFilter filter = new ProtoFilter();
        filter.setLisenter(ProtoMsgDispatcher.getInstance());

        new Thread(new Runnable() {
            @Override
            public void run() {
                tcpClientSocketThread = new TcpClientSocketThread("192.168.43.1", 30001);
                tcpClientSocketThread.setReceiveListener(new TcpClientSocketThread.ReceiveListener() {
                    @Override
                    public void OnDataReceive(byte[] bytes, int i) {
                        if(bytes.length>0 && i>0){
                            filter.onDataFiltering(bytes, i);
                        }
                    }
                });

                tcpClientSocketThread.setStateListener(new TcpClientSocketThread.TcpClientStateListener() {
                    @Override
                    public void onTcpClientReconnect(TcpClientSocketThread tcpClientSocketThread, String s, int i) {

                    }

                    @Override
                    public void onTcpClientConnectionOk(TcpClientSocketThread tcpClientSocketThread, String s, InetAddress inetAddress, int i) {
                        deviceConnected = true;
                    }

                    @Override
                    public void onTcpClientFailedConnection(TcpClientSocketThread tcpClientSocketThread, String s, int i) {
                        deviceConnected = false;
                    }
                });
                tcpClientSocketThread.connect();
            }
        }).start();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }
}
